package com.lotter.context;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lotter.common.context.YcContext;
import com.lotter.common.enumobject.OperationType;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.DateUtil;
import com.lotter.common.util.HttpUtil;
import com.lotter.common.util.StringUtil;
import com.lotter.model.caipiao.CashRecord;

public class CaipiaoKongContent {
	
	//今天比赛数据
	private static final Map<String,String> CAIPIAO_DATA = new ConcurrentHashMap<String, String>();
	
	//matchid 集合  用，号隔开
	private static String today_matchIds ="";
	private static String tomorrow_matchIds ="";
	private static String third_matchIds ="";
	//当天数据
	public  static void assembling_TODAY() throws Exception{
		TODAY_date();
	}
	
	//明天数据
	public  static void assembling_TOMORROW() throws LotterCusException{
		TOMORROW_date();
	}
	
	//后天数据
	public  static void assembling_THIRD() throws LotterCusException{
		THIRD_date();
	}
	
    //获取当前数据
	public  static  Map<String,String> findCaipiaoData() throws Exception{  
        return CAIPIAO_DATA;  
	}
	
	public static String  findMapData(String key){
		return CAIPIAO_DATA.get(key);
	}
	
	//获取当天场次ID
	public static String get_today_matchIds(){
		return today_matchIds;
	}
	
	//获取明天场次ID
	public static String get_tomorrow_matchIds(){
		return tomorrow_matchIds;
	}
	
	//获取后天场次ID
	public static String get_third_matchIds(){
		return third_matchIds;
	}
	
	 /**
	  * 去掉超过时间的
	  * @param resukt
	  */
	 private static  void TODAY_date() throws Exception {  
		 String cdate = DateUtil.date2String(new Date(),YcContext.PATTERN_CONNECT);
		 String  message = getMessage(cdate);
		 if(StringUtil.isBlank(message))
			 return ;
		 CAIPIAO_DATA.put("today_data", message);
         CAIPIAO_DATA.remove("today_data");
         
         List<CashRecord> list =JSON.parseArray(message, CashRecord.class);
    	 if(CollectionUtils.isEmpty(list))
    		 return ;
    	 
    	 Iterator<CashRecord> stuIter = list.iterator();  
    	 Date date =null ;
    	 StringBuffer buffer = new StringBuffer();
    	 while (stuIter.hasNext()) {
    		 CashRecord cashRecord = stuIter.next();  
    		 buffer.append(cashRecord.getId()+",");
    		 date = DateUtil.stringToDate(cashRecord.getTime_endsale(), YcContext.DATE_STR_PATTERN);
    		 if(date.getTime() <  new Date().getTime() + YcContext.PASS_TIME ){
    			 stuIter.remove();  
			 }
         }  
    	 today_matchIds = buffer.toString();
    	 CAIPIAO_DATA.put("today_data", JSON.toJSONString(list));
    	 CAIPIAO_DATA.put("today_num", ""+list.size());
    	 CAIPIAO_DATA.put("today_time", cdate);
     }
	 
	 //明天的场次
	 private static void TOMORROW_date() throws RuntimeException {  
		 Date nextdate = DateUtil.getDateplusDay(new Date(),1,null,null,null);
    	 String date = DateUtil.date2String(nextdate,YcContext.PATTERN_CONNECT);
         String  message = getMessage(date);
         if(StringUtil.isBlank(message))
			 return ;
         CAIPIAO_DATA.remove("tomorrow_data");
         List<CashRecord> list =JSON.parseArray(message, CashRecord.class);
         StringBuffer buffer = new StringBuffer();
         for (CashRecord cashRecord: list) {
        	 buffer.append(cashRecord.getId()+",");
		 }
         tomorrow_matchIds =buffer.toString();
         CAIPIAO_DATA.put("tomorrow_data", JSON.toJSONString(list));
         CAIPIAO_DATA.put("tomorrow_num", ""+list.size());
         CAIPIAO_DATA.put("tomorrow_time",  DateUtil.date2String(nextdate,YcContext.DATE_STR_YMD));
	 }
	 
	//装配数据
     private static void THIRD_date() throws RuntimeException {  
		  Date nextdate = DateUtil.getDateplusDay(new Date(),2,null,null,null);
	      String date = DateUtil.date2String(nextdate,YcContext.PATTERN_CONNECT);
	      String  message = getMessage(date);
	      if(StringUtil.isBlank(message))
				 return ;
	      CAIPIAO_DATA.remove("third_data");
	      List<CashRecord> list =JSON.parseArray(message, CashRecord.class);
	      StringBuffer buffer = new StringBuffer();
          for (CashRecord cashRecord: list) {
        	 buffer.append(cashRecord.getId()+",");
		  }
          third_matchIds =buffer.toString();
	      CAIPIAO_DATA.put("third_data", JSON.toJSONString(list));
	      CAIPIAO_DATA.put("third_num", ""+list.size());
	      CAIPIAO_DATA.put("third_time",  DateUtil.date2String(nextdate,YcContext.DATE_STR_YMD));
	 }
	
	//验证  ptype  itemtype
	public static Boolean  VerPtypeAndItemtype(Integer ptype , Integer itemtype) throws LotterCusException{
		if(ptype == null || itemtype == null){
			return false ;
		}
		if(ptype == OperationType.current_spf.getCode() ||
		   ptype == OperationType.current_rqspf.getCode()){
			int[] current_spf = { 0, 1 , 3 };
			if(!ArrayUtils.contains(current_spf, itemtype)){
				return false;
			}
		} //总进球 8 
		else if(ptype == OperationType.current_jqs.getCode()){
			int[] current_jqs = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
			if(!ArrayUtils.contains(current_jqs, itemtype)){
				return false;
			}
		}//半全场 9 
		else if(ptype == OperationType.current_bqc.getCode()){
			int[] current_bqc = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9};
			if(!ArrayUtils.contains(current_bqc, itemtype)){
				return false;
			}
		}//比分
		else if(ptype == OperationType.current_bf.getCode()){
			int[] current_bf = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
					18,19,20,21,22,23,24,25,26,27,28,29,30,31};
			if(!ArrayUtils.contains(current_bf, itemtype)){
				return false;
			}
		}else{
			return false;
		}
		return true ;
	} 
	
	
	//验证 彩票站 matchId 
	public static boolean VerMatchId(String pid) throws LotterCusException{
		if(pid == null){
			return false ;
		}
		if(StringUtil.isNotBlank(today_matchIds)){
			if(StringUtil.contains(today_matchIds, pid)){
				return true ;
			}
		}
		if(StringUtil.isNotBlank(tomorrow_matchIds)){
			if(StringUtil.contains(tomorrow_matchIds, pid)){
				return true ;
			}
		}
		if(StringUtil.isNotBlank(third_matchIds)){
			if(StringUtil.contains(third_matchIds, pid)){
				return true ;
			}
		}
		return false;
	}
	
	
	

	//获取彩票站数据
	public static   String getMessage(String date) throws LotterCusException{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", "jczq");
		params.put("format", "json");
		params.put("uid", "426945");
		params.put("token", "176b2a95b4bbf51ea5c5368c8b8f8e2fe44870a7");
		params.put("phase", date);
		String message = HttpUtil.get("http://api.caipiaokong.com/live/", params);
		if(message.length() < 50){
			return null ;
		}
		return message;
	}
	
	private CaipiaoKongContent() {
		super();
	}

	public static void main(String[] args) {
	   String message =getMessage("20161218");
	   System.out.println(message);
	   List<CashRecord> list =JSON.parseArray(message, CashRecord.class);
	   
	 /*  for(CashRecord cashRecord : list){
		   if("*".equals(cashRecord.getFirst_half())){
			   System.out.println(cashRecord.getId());
			   System.out.println(cashRecord.getFirst_half().length());
			   System.out.println(cashRecord.getMatch_name());
		   }
		   if("".equals(cashRecord.getFirst_half())){
			   System.out.println(cashRecord.getId());
			   System.out.println(cashRecord.getMatch_name());
			   System.out.println(cashRecord.getFirst_half().length());
		   }else{
			 JSONObject obj =  JSON.parseObject(cashRecord.getCurrent_bf());
			 System.out.println(obj.get("1"));
		   }
		   
	   }
	   */
	}
}
